package com.yndsoft.jc.supplier.hr;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.ContactAdapter;
import com.yndsoft.jc.supplier.adapter.EntryAdapter;
import com.yndsoft.jc.supplier.adapter.HolidayAdapter;
import com.yndsoft.jc.supplier.adapter.SignAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.ContactModel;
import com.yndsoft.jc.supplier.model.EntryModel;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.model.HolidayModel;
import com.yndsoft.jc.supplier.modelput.Sign;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class HrActivity extends SupplierBaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private String tag;
    private TextView barTitle;

    @Override
    public int getContentResId() {
        return R.layout.supplier_bar_recycler;
    }

    public static void start(Context context, String tag) {
        Intent intent = new Intent(context, HrActivity.class);
        intent.putExtra(Constants.TAG, tag);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        barTitle = findViewById(R.id.bar_title);
        ImageButton barAdd = findViewById(R.id.bar_add);
        recyclerView = findViewById(R.id.recycler_view);

        barAdd.setVisibility(View.VISIBLE);
        barBack.setOnClickListener(this);
        barAdd.setOnClickListener(this);
    }

    @Override
    public void initData() {
        tag = getIntent().getStringExtra(Constants.TAG);
        if (tag != null) {
            setView(tag);
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onEvent(EventModel model) {
        if (model != null && model.getInteger() == Constants.HR_ADD && tag != null) {
            loadTag(tag);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        onEvent(new EventModel());
    }

    private void setView(String tag) {
        switch (tag) {
            case Constants.HOLIDAY:
                barTitle.setText("假期列表");
                break;
            case Constants.CONTACT:
                barTitle.setText("联系人");
                break;
            case Constants.ENTRY:
                barTitle.setText("录入员");
                break;
            case Constants.SIGN:
                barTitle.setText("审核员");
                break;
        }
        loadTag(tag);
    }

    private void loadTag(String tag) {
        switch (tag) {
            case Constants.HOLIDAY:
                loadHoliday();
                break;
            case Constants.CONTACT:
                loadContact();
                break;
            case Constants.ENTRY:
                loadEntry();
                break;
            case Constants.SIGN:
                loadSign();
                break;
        }
    }

    private void loadHoliday() {
        mHttpUtil.supplierHoliday(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                String msg = returnMsg.getInfo();
                if (msg != null) {
                    List<HolidayModel> model = new Gson().fromJson(msg, new TypeToken<List<HolidayModel>>() {}.getType());
                    if (model != null && model.size() > 0) {
                        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                        recyclerView.setAdapter(new HolidayAdapter(mContext, model));
                    }
                }
            }
        }, "holidaylist", mSession.getSid());
    }

    private void loadContact() {
        mHttpUtil.supplierHoliday(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                String msg = returnMsg.getInfo();
                if (msg != null) {
                    List<ContactModel> model = new Gson().fromJson(msg, new TypeToken<List<ContactModel>>() {
                    }.getType());
                    if (model != null && model.size() > 0) {
                        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                        recyclerView.setAdapter(new ContactAdapter(mContext, model));
                    }
                }
            }
        }, "contactlist", mSession.getSid());
    }

    // 1表示系统管理员，2表示录入员，3表示审核员
    private void loadEntry() {
        mHttpUtil.supplierEntry(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                String msg = returnMsg.getInfo();
                if (msg != null) {
                    List<EntryModel> model = new Gson().fromJson(msg, new TypeToken<List<EntryModel>>() {}.getType());
                    if (model != null && model.size() > 0) {
                        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                        recyclerView.setAdapter(new EntryAdapter(mContext, model));
                    }
                }
            }
        }, "adminlist", mSession.getSid(), 2);
    }

    private void loadSign() {
        mHttpUtil.supplierEntry(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                String msg = returnMsg.getInfo();
                if (msg != null) {
                    List<EntryModel> model = new Gson().fromJson(msg, new TypeToken<List<EntryModel>>() {}.getType());
                    if (model != null && model.size() > 0) {
                        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                        recyclerView.setAdapter(new SignAdapter(mContext, model));
                    }
                }
            }
        }, "adminlist", mSession.getSid(), 3);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.bar_add:
                if (tag != null) {
                    switch (tag) {
                        case Constants.HOLIDAY:
                            AddUpdateActivity.start(mContext, Constants.HOLIDAY, Constants.ADD);
                            break;
                        case Constants.CONTACT:
                            AddUpdateActivity.start(mContext, Constants.CONTACT, Constants.ADD);
                            break;
                        case Constants.ENTRY:
                            AddUpdateActivity.start(mContext, Constants.ENTRY, Constants.ADD);
                            break;
                        case Constants.SIGN:
                            AddUpdateActivity.start(mContext, Constants.SIGN, Constants.ADD);
                            break;
                    }
                }
                break;
        }
    }
}
